<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-06-04 17:19
 *
 * 项目：rm  -  $  - generateHelper.php
 *
 * 作者：liwei 
 */

namespace modules\levmodules\helpers;


use Lev;
use lev\helpers\cacheFileHelpers;

!defined('INLEV') && exit('Access Denied LEV');

class replaceFileContentHelper
{

    public static $generateFiles;
    public static $unAddDz = [];

    /**
     * 此处设置生成模块文件夹时需要替换掉的变量
     * @param $vars
     * @return array
     */
    public static function replaceVars($vars = []) {
//        $vars['!defined(\'INLEV\') && exit'] = '!defined(\'IN_DISCUZ\') && exit';
//        $vars['!defined(\'INLEV\') &&exit'] = '!defined(\'IN_DISCUZ\') && exit';
//        $vars['!defined(\'INLEV\')&& exit'] = '!defined(\'IN_DISCUZ\') && exit';
//        $vars['!defined(\'INLEV\')&&exit'] = '!defined(\'IN_DISCUZ\') && exit';
        return $vars;
    }

    /**
     * @param $zipPath
     * @param $replaceParam
     * @param $replacePath
     * @param bool $repname
     * @param bool $addDz
     * @return array
     */
    public static function createReplaceFiles($zipPath, $replaceParam, $replacePath, $repname = false, $addDz = false) {
        $zipPath = Lev::getAlias($zipPath);
        $arr = glob($zipPath . '/*');
        if (empty($arr)) {
            return Lev::responseMsg(-1, '无文件：'.$zipPath);
        }
        $replacePath = rtrim('/__replace_files.bak/'.$replacePath, '/');
        //$toZipPath = rtrim($zipPath, '/'). $replacePath;
        $toZipPath = Lev::$aliases['@modules']. $replacePath;
        if (is_dir($toZipPath)) {
            cacheFileHelpers::rmdirv($toZipPath);
        }
        cacheFileHelpers::mkdirv($toZipPath);
        $replaces = static::replaceVars($replaceParam);
        static::doGenerateFiles($zipPath, $toZipPath, array_keys($replaces), array_values($replaces), $repname, $addDz);
        return Lev::responseMsg(1, '', ['files'=>static::$generateFiles, 'replacePath'=>$toZipPath.'/', 'unAddDz'=>static::$unAddDz]);
    }

    public static function doGenerateFiles($sourceDir, $toDir, $search, $replace, $repname = false, $addDz = false) {
        $files = glob(rtrim($sourceDir, '/') . '/*');
        foreach ($files as $src) {
            if ($src != $toDir && strpos($src, '__replace_files.bak') === false && !zipHelper::checkExclude($src, '', '')) {
                $name = !$repname ? basename($src) : str_replace($search, $replace, basename($src));
                $file = $toDir . '/' . $name;
                static::$generateFiles[] = $file;
                if (is_dir($src)) {
                    cacheFileHelpers::mkdirv($file);
                    self::doGenerateFiles($src, $file, $search, $replace, $repname, $addDz);
                } else {
                    $data = str_replace($search, $replace, file_get_contents($src));
                    if ($addDz && stripos($data, '<?php') === 0) {
                        $addString = $addDz === 'INLEV' ? '!defined(\'INLEV\')' : '!defined(\'IN_DISCUZ\')';
                        if (stripos($data, $addString) === false) {
                            $addindiscuz = $addString.' && exit(\'Access Denied\');';
                            if (stripos($data, 'namespace ') === false) {
                                $data = '<?php ' . $addindiscuz . substr($data, 5);
                            } else {
                                $data = static::setNsIN_DISCUZ($data, $addindiscuz);
                                static::$unAddDz[] = $src;
                            }
                        }
                    }
                    file_put_contents($file, $data);
                }
            }
        }
    }

    public static function setNsIN_DISCUZ($data, $addindiscuz) {
        $arr = explode('namespace ', $data);
        $endK = count($arr) - 1;
        $end = explode(';', $arr[$endK], 2);
        $arr[$endK] = $end[0].";\n".$addindiscuz.$end[1];
        return implode('namespace ', $arr);
    }

}